package com.platform.modules.chat.utils;

import cn.hutool.core.util.ReUtil;
import com.platform.common.exception.BaseException;
import com.platform.modules.chat.config.TencentConfig;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.tmt.v20180321.TmtClient;
import com.tencentcloudapi.tmt.v20180321.models.SpeechTranslateRequest;
import com.tencentcloudapi.tmt.v20180321.models.SpeechTranslateResponse;
import com.tencentcloudapi.tmt.v20180321.models.TextTranslateRequest;
import com.tencentcloudapi.tmt.v20180321.models.TextTranslateResponse;

/**
 * 腾讯工具类
 */
public class TencentUtils {

    /**
     * 腾讯翻译
     */
    public static String translation(TencentConfig tencentConfig, String content) {
        String target = "zh";
        String source = "auto";
        if (ReUtil.contains("[\\u4e00-\\u9fa5]", content)) {
            target = "en";
            source = "zh";
        }
        Credential cred = new Credential(tencentConfig.getAppKey(), tencentConfig.getAppSecret());
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("tmt.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        TmtClient client = new TmtClient(cred, "ap-beijing", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        TextTranslateRequest req = new TextTranslateRequest();
        req.setSourceText(content);
        req.setSource(source);
        req.setTarget(target);
        req.setProjectId(0L);
        TextTranslateResponse resp;
        try {
            resp = client.TextTranslate(req);
        } catch (TencentCloudSDKException e) {
            throw new BaseException("翻译机器人接口调用异常，请稍后再试");
        }
        StringBuilder builder = new StringBuilder();
        builder.append("翻译结果：");
        builder.append("\n");
        builder.append("原文：");
        builder.append(content);
        builder.append("\n");
        builder.append("译文：");
        builder.append(resp.getTargetText());
        return builder.toString();
    }

    /**
     * 语音识别
     */
    public static String audio2Text(TencentConfig tencentConfig, String content) {
        try {
            Credential cred = new Credential(tencentConfig.getAppKey(), tencentConfig.getAppSecret());
            ClientProfile clientProfile = new ClientProfile();
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("tmt.tencentcloudapi.com");
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            TmtClient client = new TmtClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SpeechTranslateRequest req = new SpeechTranslateRequest();
            req.setSessionUuid("1");
            req.setSource("zh");
            req.setTarget("zh");
            req.setAudioFormat(83886080L);
            req.setSeq(0L);
            req.setIsEnd(1L);
            req.setData(content);
            SpeechTranslateResponse resp = client.SpeechTranslate(req);
            return resp.getSourceText();
        } catch (Exception e) {
            throw new BaseException("语音识别接口调用异常，请稍后再试");
        }
    }
}
